package com.yumeng.framework.persistence.mybatis.handlers;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.yumeng.common.auth.BaseAuthHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;

import java.time.LocalDateTime;

/**
 * 元对象字段填充控制器
 *
 * @author wxd
 * @date 2021/12/14 15:01
 */
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {
    /**
     * 插入元对象字段填充（用于插入时对公共字段的填充）
     *
     * @param metaObject 元对象
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        log.debug("start insert fill ....");
        this.strictInsertFill(metaObject, "creater", Long.class, BaseAuthHelper.getCurrAuthInfo(false).getIdentityIdLongVal());
        this.strictInsertFill(metaObject, "createTime", LocalDateTime :: now, LocalDateTime.class);
    }

    /**
     * 更新元对象字段填充（用于更新时对公共字段的填充）
     *
     * @param metaObject 元对象
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        log.debug("start update fill ....");
        this.strictUpdateFill(metaObject, "updater", Long.class, BaseAuthHelper.getCurrAuthInfo(false).getIdentityIdLongVal());
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime :: now, LocalDateTime.class);
    }
}
